package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.example.com.skills_utf8.fragment32_1.Adapter;
import com.example.com.skills_utf8.fragment32_1.ChildBean;
import com.example.com.skills_utf8.fragment32_1.GroupViewHolder;
import com.example.com.skills_utf8.fragment32_1.ParentBean;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;







public class ExpandableListView33Activity extends Activity {

	ExpandableListView33Activity activity = this;
	
	
	
	ArrayList<GroupBean>    bean = new ArrayList<GroupBean>();;   
	DataAdapter adapter;
	private ExpandableListView listview;
	
	
	
	
	void add_data(){
		
		
		for(int i =0; i<2; i++){
			GroupBean parent = new GroupBean();
			parent.setName("标题" + i);
			parent.child = new ArrayList<ChildBean>();
			for(int j =0; j<3; j++){
				ChildBean child = new ChildBean();
				child.setName("第"+j);
				parent.child.add(child);
			}
			
			bean.add(parent);
		}
		
		
		/*
		ChildBean child = new ChildBean();
		child.setName("第一行");
		child.setName("第二行");
           
        for (int  index = 0 ; index <2; ++index)   
        {   
        	GroupBean parent = new GroupBean();  
        	parent.setName("第" + index);
            parent.setChild(child); 
               
            bean.add(parent);   
        }
		*/
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_32);
		
		
		 listview = (ExpandableListView)findViewById(R.id.expandable_list_view);
		
		
	    add_data(); 
        
			adapter = new DataAdapter( activity, bean );
			listview.setAdapter(adapter);
			
		Button btn_add_data = (Button)findViewById(R.id.btn_add_data);
		btn_add_data.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
		
				
				adapter.addData("标题", "子项");
				
			}
		});
		
		
		
		
	}
	
	
	
	 class GroupBean {
		private String name;
		private ArrayList<ChildBean> child;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public ArrayList<ChildBean> getChild() {
			return child;
		}

		public void setChild(ArrayList<ChildBean> child) {
			this.child = child;
		}
	}
	
	
	 class ChildBean {
		private String name;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
	


 class DataAdapter extends BaseExpandableListAdapter {

	private LayoutInflater group;
	private LayoutInflater child;
	private ArrayList<GroupBean> data;

	public DataAdapter(Context context, ArrayList<GroupBean> data) {
		group = LayoutInflater.from(context);
		child = LayoutInflater.from(context);
		this.data = data;
	}

///*
	//添加一个数据
	public void addData(String groupdata, String childdata) {
		ChildBean childbean = new ChildBean();
		childbean.setName(childdata);

		GroupBean groupbean = new GroupBean();
		groupbean.setName(groupdata);
		ArrayList<ChildBean> childlist = new ArrayList<ChildBean>();
		childlist.add(childbean);
		
		groupbean.setChild(childlist);
		
		data.add(groupbean);
		notifyDataSetChanged();
	}
	//*/

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return data.get(groupPosition).getChild().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ChildHolder holder = null;
		if (convertView == null) {
			holder = new ChildHolder();
			convertView = child.inflate(R.layout.child_item, null);
			holder.childtxt = (TextView) convertView
					.findViewById(R.id.child_item_title);
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}
		holder.childtxt.setText(data.get(groupPosition).getChild()
				.get(childPosition).getName());
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return data.get(groupPosition).getChild().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return data.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		GroupHolder holder = null;
		if (convertView == null) {
			holder = new GroupHolder();
			convertView = group.inflate(R.layout.group_item, null);
			holder.grouptxt = (TextView) convertView
					.findViewById(R.id.group_item_txt);
			convertView.setTag(holder);
		} else {
			holder = (GroupHolder) convertView.getTag();
		}
		holder.grouptxt.setText(data.get(groupPosition).getName());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	class GroupHolder {
		TextView grouptxt;
	}

	class ChildHolder {
		TextView childtxt;
	}

}

	
	
	
}
